package models

import (
	"github.com/astaxie/beego/orm"
)

type AuthModels struct{}

// 获取用户的权限列表
func (a *AuthModels) GetAuth(aid int64) ([]orm.Params, error) {
	var sql string
	var list []orm.Params
	var err error
	o := orm.NewOrm()
	if aid == 1 {
		sql = "select me.* from menu me WHERE is_menu=1 and menustatus=1"
		_, err = o.Raw(sql).Values(&list)
	} else {
		sql = "select me.* from menu me WHERE is_menu=1 and menustatus=1 and id in (select menu_id from group_menu where group_id in (select group_id FROM admin_group where admin_id = ?))"
		_, err = o.Raw(sql, aid).Values(&list)
	}
	return list, err
}

//查询用户是否有访问该路径的权限
func (a *AuthModels) IsAuth(aid int64, href string) bool {
	type idExist struct {
		Id       int64
		Authopen int
	}
	isId := idExist{}
	//没有开启验证即为允许访问，如果开启查询该用户是否有访问权限
	sql := "select me.* from menu me WHERE (authopen=0 and href=?) or (href=? and id in (select menu_id from group_menu where group_id in (select group_id FROM admin_group where admin_id = ?))) limit 1"
	o := orm.NewOrm()
	_ = o.Raw(sql, href, href, aid).QueryRow(&isId)
	if isId.Id != 0 {
		return true
	}
	return false
}
